# -*- coding: utf-8 -*-
from django.conf.urls.defaults import *
from django.conf import settings
from django.contrib import admin
from django.contrib.flatpages.models import FlatPage
from django.shortcuts import get_object_or_404
from django.views.generic.simple import direct_to_template

from news.models import News
from projects.models import Project, Release
from projects.feeds import *
from news.feeds import *
from comments.feeds import *
from cab.feeds import *
from utils.stats import stats
admin.autodiscover()

# workaround for #10061
admin.site.root_path = '/admin/'


news = News.active_objects.count() > 5 and News.active_objects.all()[:5] or News.active_objects.all()

atom_feeds = {"releases": RecentReleasesAtom,
              "news": RecentNewsAtom,
              "comments": GenericCommentsAtom,
              "potm": LatestPotmEntriesAtom,
              "snippets": SnippetsAtom,}

rss_feeds = {"releases": RecentReleasesRSS,
             "news": RecentNewsRSS,
             "comments": GenericCommentsRSS,
             "potm": LatestPotmEntriesRSS,
             "snippets": SnippetsRSS,} 

urlpatterns = patterns('',
    (r'^$', 'django.views.generic.simple.direct_to_template',
           {
            
            "template": 'news/startpage.html',
            "extra_context": {"latest_news": news,
                              "page": FlatPage.objects.get(url="/")
                              }, 
          }),
    # admin stuff
    url(r'^admin/sendmail/$',
        'notification.admin.sendmail'),
    url(r'^admin/news/news/(?P<object_id>[0-9]+)/preview/$',
        'news.views.preview'),
    (r'^admin/doc/', include('django.contrib.admindocs.urls')),
    (r'^admin/', include(admin.site.urls) ),

    # auth stuff,
    # (r'^accounts/login/$', 'django.contrib.auth.views.login'), # part of registration
    # registration 
    (r'^accounts/', include('registration.urls')),
    # profiles
    (r'^profiles/', include('profiles.urls')),
    # feeds
    url(r'^feeds/atom/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
          {'feed_dict': atom_feeds}, name="atom_feeds"),
    url(r'^feeds/rss/(?P<url>.*)/$', 'django.contrib.syndication.views.feed',
          {'feed_dict': rss_feeds}, name="rss_feeds"),          
    # apps
    (r'^news/', include('news.urls')),
    (r'^projects/', include('projects.urls')),
    url(r'^xmlrpc/$', 'pygameweb.notification.views.rpc_handler', name="xmlrpc"),
    (r'^snippets/', include('cab.urls')),
    # searching
    (r'^search/', include('haystack.urls')),

    
    # api
    (r'^api/', include('api.urls')),
    
    # redirect old urls
    url(r"^project/(?P<project_id>[0-9]+)/$" , "utils.redirects.old_project",
        name="old_project_detail"),
    # tiny urls
    url("^(?P<tiny_bit>[spurn])/(?P<obj_id>.+?)/$", 
        "utils.redirects.generic_tiny_url", name="tiny_url"), 
                
    (r'docs/$', 'django.views.generic.simple.direct_to_template',
           {
            
            "template": 'docs/index.html',
            
          }),
    (r'^docs/(?P<name>.+?).html$', 
        lambda r,name:direct_to_template(r, 'docs/%s.html'% name)),
    
    # some stats
     url(r'^stats/$', 'django.views.generic.simple.direct_to_template',
           {"template": 'utils/stats.html',
            "extra_context": {"stats": stats},}, name="stats"),
    

)

if settings.DEBUG:
    urlpatterns += patterns('', (r'^%s(?P<path>.*)$' % settings.MEDIA_URL[1:], 'django.views.static.serve',
        {'document_root': settings.MEDIA_ROOT, 'show_indexes': True}),)

